package com.group.Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group.pojo.GClass;
import com.group.pojo.GStudent;
import com.group.pojo.GStudentPage;
import com.group.pojo.GUser;

@WebServlet("/ClassInfoServlet")
public class ClassInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public ClassInfoServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		
		//获取班级信息列表
		if(action.equals("info")) {
			GClass GClass = new GClass();
			List<GClass> class_list = GClass.getGClassList();
			request.setAttribute("classList", class_list);
			request.getRequestDispatcher("class.jsp").forward(request, response);
		}
		//获取班级学生列表
		else if(action.equals("studentList")) {
			String pageNo = request.getParameter("pageNo");
			if(pageNo == null || "".equals(pageNo)) {
				pageNo = "1";
			}
			GStudentPage pagination = new GStudentPage();
			pagination.setPageNo(Integer.parseInt(pageNo));
			GClass c = new GClass();
			//固定班级ID为130265
			String classID = "130265";
			int recordCount = c.getRecordCountByID(classID);
			int pageSize = pagination.getPageSize();
			pagination.setTotalPages((recordCount + pageSize - 1) / pageSize);
			List<GStudent> student_list = pagination.getPageData(classID);
			request.setAttribute("studentList", student_list);
			request.setAttribute("pagination", pagination);
			request.getRequestDispatcher("student-list.jsp").forward(request, response);
			
//			String classID = request.getParameter("classid");
//			GClass GClass = new GClass();
//			List<GStudent> student_list = GClass.getGStudentListByID(classID);
//			request.setAttribute("studentList", student_list);
//			request.getRequestDispatcher("student-list.jsp").forward(request, response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		//更新班级信息
		if(action.equals("update")) {
			String classID = request.getParameter("classID");
			String classYear = request.getParameter("classYear");
			String className = request.getParameter("className");
			GClass GClass = new GClass(classID, className, Integer.parseInt(classYear));
			GClass.updateGClassInfo(GClass);
			response.sendRedirect("ClassInfoServlet?action=info");
		}
		//更改密码
		if(action.equals("alterPwd")){
			GUser user = (GUser) request.getSession().getAttribute("SESSION_USER");
			String oldPwd = request.getParameter("oldPwd");
			String newPwd = request.getParameter("newPwd");
			GUser u = new GUser(user.getUserID(),oldPwd);
			if(u.login()){
				u.updatePwd(user.getUserID(),newPwd);
			}
			response.sendRedirect("ClassInfoServlet?action=info");
		}
	}
}
